package com.bw.module_merchant.vm

import android.util.Log


import com.bw.module_home.api.MerchantApiServer
import com.bw.module_home.model.MerchantRepository
import com.bw.module_home.model.MerchantResultData
import com.bw.module_merchant.entity.EnterpriseEntityItem
import com.example.lib_common.view.vm.BaseViewModel

import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class EnterpriseViewModel @Inject constructor(
    private val mService: MerchantApiServer
) : BaseViewModel<MerchantRepository, MerchantResultData<List<EnterpriseEntityItem>>>() {

    fun getEnterpriseList(){
        mViewModelScope.launch { 
            try {

                val result = mService.getEnterpriseList()
                val res = MerchantResultData(
                    state = result.state,
                    data = result.data,
                    msg = result.msg
                )

                mSuccessLiveData.postValue(res)

            }catch (e:Exception){
                Log.d("EnterpriseViewModel", "getEnterpriseList: 请求失败${e.message}")
                mErrorLiveData.postValue("请求失败${e.message}")
            }
        }
    }

    fun getEnterpriseDetail(id:Int){
        mViewModelScope.launch {
            try {
                val result = mService.getEnterpriseDetail(id)
                val res = MerchantResultData(
                    state = result.state,
                    data = listOf(result.data),
                    msg = result.msg
                )

                mSuccessLiveData.postValue(res)

            }catch (e:Exception){
                Log.d("EnterpriseViewModel", "getEnterpriseDetail: 请求失败${e.message}")
                mErrorLiveData.postValue("请求失败${e.message}")
            }
        }
    }



}